package core;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import common.Sequence;
import common.Service;
import common.Servlet;
import common.Validate;
import dict.BmDict;
import dict.CommonDict;
import dict.TransactionDict;
import dicts.GlobalDict;
import dicts.PaymentDict;
import entities.TBm;
import entities.TMerchant;
import interfaces.IBmService;
import interfaces.IMerchantService;
import net.sf.json.JSONObject;
import util.LogUtil;
import utils.DateUtil;
import utils.StringUtil;

public class Deposit extends Servlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public synchronized void process(HttpServletRequest request, HttpServletResponse response) throws Exception {

		System.out.println(DateUtil.format(new Date(), DateUtil.DATETIME) + " | 商户预存");
		//// 初始化service
		IMerchantService merchantService = (IMerchantService) Service.create(IMerchantService.class);
		IBmService bmService = (IBmService) Service.create(IBmService.class);

		Map<String, Object> paramMap = Collections.synchronizedMap(new HashMap<String, Object>());
		JSONObject jsonResult = new JSONObject();

		// 初始化bean和属性
		TMerchant merchant = new TMerchant();
		TBm bm = new TBm();

		// 验证系统
		if (GlobalDict.LINUX.equalsIgnoreCase(System.getProperty("os.name")) && !CommonDict.HOST.equals(request.getServerName())) {
			response.getWriter().print(Validate.result(TransactionDict.ILLAGEL_ACCESS, TransactionDict.MESSAGES.get(TransactionDict.ILLAGEL_ACCESS)));
			return;
		}

		// 校验参数 ( 名称,值,校验模式,边界 )
		List<Object[]> parameterList = Collections.synchronizedList(new ArrayList<Object[]>());
		parameterList.add(new Object[] { "merNo", request.getParameter("merNo"), Validate.NOTEMPTY | Validate.EXCEEDLENGTH, 0, 50 });
		parameterList.add(new Object[] { "bmAmount", request.getParameter("bmAmount"), Validate.NOTEMPTY | Validate.ISNUMBER | Validate.EXCEEDNUMBER, 0, Integer.MAX_VALUE });
		parameterList.add(new Object[] { "bmRemark", request.getParameter("bmRemark"), Validate.EXCEEDLENGTH, 0, 100 });

		JSONObject validateResult = Validate.validate(parameterList);
		LogUtil.deposit(Validate.summary(parameterList));

		if (!StringUtil.isEmpty(validateResult)) {
			System.out.println ( validateResult.toString() ) ;
			response.getWriter().print(validateResult.toString());
			return;
		}

		bm.setBmAmount(new Double(request.getParameter("bmAmount")));
		bm.setBmRemark(request.getParameter("bmRemark"));

		// 交易商户存在和状态
		try {
			merchant = merchantService.fetch(request.getParameter("merNo"));
		} catch (Exception e) {
			response.getWriter().print(Validate.result(TransactionDict.MERCHANT_FETCH_EXCEPTION, TransactionDict.MESSAGES.get(TransactionDict.MERCHANT_FETCH_EXCEPTION)));
			return;
		}

		if (merchant == null) {
			response.getWriter().print(Validate.result(TransactionDict.MERCHANT_NOT_EXIST, TransactionDict.MESSAGES.get(TransactionDict.MERCHANT_NOT_EXIST)));
			return;
		}

		if (!merchant.getMerState().equals("1")) {
			response.getWriter().print(Validate.result(TransactionDict.MERCHANT_STATUS_EXCEPTION, TransactionDict.MESSAGES.get(TransactionDict.MERCHANT_STATUS_EXCEPTION)));
			return;
		}

		// 提现存在状态(是否之前还有未处理的项)
		try {
			paramMap = Collections.synchronizedMap(new HashMap<String, Object>());
			paramMap.put("merNo", merchant.getMerNo());
			paramMap.put("bmVstate", "0");
		} catch (Exception e) {
			response.getWriter().print(Validate.result(BmDict.BM_FETCH_EXCEPTION, BmDict.MESSAGES.get(BmDict.BM_FETCH_EXCEPTION)));
			return;
		}

		if (bmService.contain(paramMap)) {
			response.getWriter().print(Validate.result(BmDict.BM_ALREADY_EXIST, BmDict.MESSAGES.get(BmDict.BM_ALREADY_EXIST)));
			return;
		}

		try {
			bm.setBmNo(Sequence.bmSequence());
			bm.setMerNo(merchant.getMerNo());
			bm.setBmType(PaymentDict.BM_DEPOSIT);
			bm.setBmState("1");
			bm.setBmDate(new Date());
			bm.setBmVstate("1");
			bm.setBmVdate(new Date());
			bmService.persist(bm);
		} catch (Exception e) {
			response.getWriter().print(Validate.result(BmDict.BM_PERSIST_EXCEPTION, BmDict.MESSAGES.get(BmDict.BM_PERSIST_EXCEPTION)));
			return;
		}

		jsonResult.put("code", BmDict.BM_SUCCESS);
		jsonResult.put("message", BmDict.MESSAGES.get(BmDict.BM_SUCCESS));
		response.getWriter().print(jsonResult.toString());
		return;
	}
}
